import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class LoginService extends BaseService<any> {
  private readonly logger = new Logger(LoginService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * login
   * 对应 PHP: LoginService_admin::login()
   * 逻辑类型: undefined - undefined
   */
  async login(username: string, password: string, app_type: string) {
    // 基于PHP真实逻辑: login
    // PHP原文: if(!array_key_exists($app_type, AppTypeDict::getAppType())) throw new AuthException('APP_TYPE_NOT_EXIST');          $this->site_id = $this->request->a...
if(!array_key_exists(app_type, AppTypeDict.getAppType())) throw new AuthBusinessException('APP_TYPE_NOT_EXIST'];

        this.site_id = this.request.adminSiteId(];

        const config = this.configService.getConfig();
        switch(app_type){
            case AppTypeDict.SITE:
                const is_captcha = config.is_site_captcha;
                break;
            case AppTypeDict.ADMIN:
                const is_captcha = config.is_captcha;
                break;
  }

  /**
   * logout
   * 对应 PHP: LoginService_admin::logout()
   * 逻辑类型: undefined - undefined
   */
  async logout() {
    // 基于PHP真实逻辑: logout
    // PHP原文: self::clearToken($this->uid, $this->app_type, $this->request->adminToken());         return true;     }      /**      * 创建token      * @param SysUser ...
self.clearToken(this.uid, this.app_type, this.request.adminToken()];
        return true;
    }

    /**
     * 创建token
     * @param SysUser userinfo
     * @param string app_type
     * @return */
    async createToken(SysUser userinfo, string app_type)
    {
        const expire_time = env('system.admin_token_expire_time') || 3600;
        return TokenAuth.createToken(userinfo.uid, AppTypeDict.ADMIN, [uid: userinfo.uid, username: userinfo.username), expire_time];
    }

    /**
     * 清理token
     * @param int uid
     * @param string|null type
     * @param string|null token
     */
    public static function clearToken(int uid, ?string type = '', ?string token = '')
    {
        if (!type)) {
            TokenAuth.clearToken(uid, AppTypeDict.ADMIN, token];//清除平台管理端的token
//            TokenAuth.clearToken(uid, AppTypeDict.SITE, token);//清除站点管理端的token
  }

  /**
   * createToken
   * 对应 PHP: LoginService_admin::createToken()
   * 逻辑类型: undefined - undefined
   */
  async createToken(userinfo: string, app_type: string) {
    // 基于PHP真实逻辑: createToken
    // PHP原文: $expire_time = env('system.admin_token_expire_time') ?? 3600;         return TokenAuth::createToken($userinfo->uid, AppTypeDict::ADMIN, ['uid' => $use...
const expire_time = env('system.admin_token_expire_time') || 3600;
        return TokenAuth.createToken(userinfo.uid, AppTypeDict.ADMIN, [uid: userinfo.uid, username: userinfo.username), expire_time];
    }

    /**
     * 清理token
     * @param int uid
     * @param string|null type
     * @param string|null token
     */
    public static function clearToken(int uid, ?string type = '', ?string token = '')
    {
        if (!type)) {
            TokenAuth.clearToken(uid, AppTypeDict.ADMIN, token];//清除平台管理端的token
//            TokenAuth.clearToken(uid, AppTypeDict.SITE, token);//清除站点管理端的token
  }

  /**
   * clearToken
   * 对应 PHP: LoginService_admin::clearToken()
   * 逻辑类型: undefined - undefined
   */
  async clearToken(uid: number, type: number, token: number) {
    // 基于PHP真实逻辑: clearToken
    // PHP原文: if (empty($type)) {             TokenAuth::clearToken($uid, AppTypeDict::ADMIN, $token);//清除平台管理端的token //            TokenAuth::clearToken($uid, AppT...
if (!type)) {
            TokenAuth.clearToken(uid, AppTypeDict.ADMIN, token];//清除平台管理端的token
//            TokenAuth.clearToken(uid, AppTypeDict.SITE, token);//清除站点管理端的token
  }

  /**
   * parseToken
   * 对应 PHP: LoginService_admin::parseToken()
   * 逻辑类型: undefined - undefined
   */
  async parseToken(token: string) {
    // 基于PHP真实逻辑: parseToken
    // PHP原文: if (empty($token)) {             //定义专属于授权认证机制的错误响应, 定义专属语言包             throw new AuthException('MUST_LOGIN', 401);...
if (!token)) {
            //定义专属于授权认证机制的错误响应, 定义专属语言包
            throw new AuthBusinessException('MUST_LOGIN', 401);
  }

  /**
   * resetAdministratorPassword
   * 对应 PHP: LoginService_admin::resetAdministratorPassword()
   * 逻辑类型: undefined - undefined
   */
  async resetAdministratorPassword() {
    // 基于PHP真实逻辑: resetAdministratorPassword
    // PHP原文: $super_admin_uid = ( new SysUserRole() )->where([             [ 'site_id', '=', request()->defaultSiteId() ],             [ 'is_admin', '=', 1 ]      ...
const super_admin_uid = ( this.sysUserRoleService ).where([
            [ 'site_id', '=', request().defaultSiteId() ],
            [ 'is_admin', '=', 1 ]
        ]).value('uid'];

        const user = this.userService.find(super_admin_uid];
        user.password = create_password('123456'];
        user.save(];

        self.clearToken(super_admin_uid);
    }
}
  }
}
